package com.lry.basic.algorithm.common;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author:刘仁有
 * @desc:
 * 给定一组非负整数，重新排列它们的顺序使之组成一个最大的整数。
 * 示例 1:
 * 输入: [10,2]
 * 输出: 210
 *
 * 示例 2:
 * 输入: [3,30,34,5,9]
 * 输出: 9534330
 * @email:953506233@qq.com
 * @data:2019/12/16
 */
public class LargestNumber {
    private class LargestComparator implements Comparator<String>{
        @Override
        public int compare(String o1, String o2) {
            String order1 = o1+o2;
            String order2 = o2+o1;
            return order2.compareTo(order1);
        }
    }
    public static void main(String[] args) {
        System.out.println(new LargestNumber().largestNumber(new int[]{0,0,32,54,2,9}));
    }
    public  String largestNumber(int[] nums) {
        String[] asStrs = new String[nums.length];
        for (int i=0;i<nums.length;i++) {
            asStrs[i] = String.valueOf(nums[i]);
        }

        Arrays.sort(asStrs,new LargestComparator());

        if(asStrs[0].equals("0")){
            return "0";
        }

        String res = "";
        for (String str:asStrs) {
            res+=str;
        }
        return res;
    }
}
